// Copyright 2012, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adwords.axis.v201109.accountmanagement;

import com.google.api.ads.adwords.axis.v201109.cm.DateRange;
import com.google.api.ads.adwords.axis.v201109.info.ApiUsageInfo;
import com.google.api.ads.adwords.axis.v201109.info.ApiUsageRecord;
import com.google.api.ads.adwords.axis.v201109.info.ApiUsageType;
import com.google.api.ads.adwords.axis.v201109.info.InfoSelector;
import com.google.api.ads.adwords.axis.v201109.info.InfoServiceInterface;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.factory.AdWordsServices;
import com.google.api.ads.common.lib.auth.ClientLoginTokens;

import org.joda.time.DateTime;

import java.text.SimpleDateFormat;

/**
 * This example illustrates how to find a client customer ID for a client email.
 * We recommend to use this example as a one off to convert your identifiers to
 * IDs and store them for future use.
 *
 * Tags: InfoService.get
 *
 * @category adx-exclude
 * @author Kevin Winter
 */
public class GetClientCustomerId {

  public static void main(String[] args) throws Exception {
    // Get a ClientLogin AuthToken.
    String clientLoginToken = new ClientLoginTokens.Builder()
        .forApi(ClientLoginTokens.Api.ADWORDS)
        .fromFile()
        .build()
        .requestToken();

    // Construct an AdWordsSession.
    AdWordsSession session =
        new AdWordsSession.Builder().fromFile().withClientLoginToken(clientLoginToken).build();

    // ClientEmails to look up clientCustomerId for.
    String[] clientEmails = new String[] {"INSERT_EMAIL_ADDRESS_HERE"};

    if (session.isEnvironment(AdWordsSession.Environment.SANDBOX)) {
      // To run examples in sandbox, you needed to specify a clientCustomerId.
      // This won't work for this example, so we clear it out here.
      session.setClientCustomerId(null);
    }

    runExample(session, clientEmails);
  }

  public static void runExample(AdWordsSession session, String[] clientEmails) throws Exception {
    // Get the InfoService.
    InfoServiceInterface infoService = new AdWordsServices().get(session,
        InfoServiceInterface.class);

    // Create selector.
    InfoSelector selector = new InfoSelector();
    selector.setApiUsageType(ApiUsageType.UNIT_COUNT_FOR_CLIENTS);
    String today = new SimpleDateFormat("yyyyMMdd").format(new DateTime().minusDays(1).toDate());
    selector.setDateRange(new DateRange(today, today));
    selector.setClientEmails(clientEmails);
    selector.setIncludeSubAccounts(true);

    // Get api usage info.
    ApiUsageInfo apiUsageInfo = infoService.get(selector);
    for (ApiUsageRecord record : apiUsageInfo.getApiUsageRecords()) {
      System.out.printf("Client with email '%s' has ID '%d'.\n", record.getClientEmail(),
          record.getClientCustomerId());
    }
  }
}
